package com.starblink.android.basic.adapter

import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.starblink.android.basic.sensorsdata.extension.findParentPageSpmId
import com.starblink.android.basic.sensorsdata.extension.findParentPageSpmParams

abstract class BaseVH<M> constructor(itemView: View) :
    RecyclerView.ViewHolder(itemView) {


    abstract fun onBind(model: M, position: Int)

    private var pageSpmId: Int? = null

    protected open fun findParentPageSpmId(): Int {
        if (pageSpmId == null) {
            // 此处不使用itemView是由于还未添加到RecyclerView，导致找不到Fragment
            pageSpmId = (bindingAdapter as? BaseAdapter)?.parent?.findParentPageSpmId()
        }
        return pageSpmId!!.toInt()
    }

    private var pageSpmParams: Map<String, String>? = null

    private var paramsFound = false

    protected open fun findParentPageSpmParams(): Map<String, String>? {
        if (!paramsFound) {
            pageSpmParams = (bindingAdapter as? BaseAdapter)?.parent?.findParentPageSpmParams()
            paramsFound = true
        }
        return pageSpmParams
    }


}